﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Palsta.Core
{
    public class XslChoose : XslElement
    {

        /*
         * Constructors
         */ 
        public XslChoose(XmlNode xslNode)
            : base(xslNode)
        {
            _jsFunctionName = _functionPrefix + "choose";
        }

        /*
         * Overrides
         */
        /// <summary>
        /// Kind of a switch case structor. e.g:
        /// function xslt_choose_1(){
        ///     var result = false;
        ///     if (!result) result = xslt_when_2();
        ///     if (!result) result = xslt_when_3();
        ///     if (!result) result = xslt_otherwise();
        ///     return result;
        /// }
        /// </summary>
        /// <returns></returns>
        public override String GetFunctionDeclareScript()
        {
            String result = "function " + _jsFunctionName + "_" + _index.ToString() + "() { \r\n";
            result += "var result = '';";
            foreach (XslElement node in _xslChildNodes)
            {
                result += "\r\nif (!result) result = " + node.GetFunctionCallingScript();
            }
            result += "\r\nreturn result;\r\n";
            result += "}\r\n";
            return result;
        }
    }
}
